﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class ShipUIButtonData: ShipUIData
{
    public string m_IconPath="None";
    public int m_BindKey = 0;

    public override JsonData Save()
    {
        JsonData root = base.Save();
        root["IconPath"] = m_IconPath;
        root["BindKey"] = m_BindKey;
        return root;
    } 
    public override bool Load(JsonData root)
    {
        if (!base.Load(root))
            return false;
        IDictionary<string, JsonData> rootObj = root.ValueAsObject();

        if (rootObj.ContainsKey("IconPath"))
            m_IconPath = rootObj["IconPath"].ValueAsString();
        if (rootObj.ContainsKey("BindKey"))
            m_BindKey = rootObj["BindKey"].ValueAsInt();
        return true;
    }
}
public class ShipUIButton : ShipUI
{
    public enum OperationType
    {
        NONE,
        Press,
        PressDown,
        Release,
        Click,
        OprationTypeEnd
    }
    public Sprite sprite { 
        get {
            var image = GetComponent<Image>();
            if (image)
                return image.sprite;
            return null;
                }
        set {
            var image = GetComponent<Image>();
            if (image)
                image.sprite = value;
        }
    }
    public string iconPath {
        get
        {
            var shipData = m_ShipUIData as ShipUIButtonData;
            if (Utils.CheckReferenceIsNull(shipData))
                return "";
            return shipData.m_IconPath; 
        }
        set {
            if (GetComponent<Image>())
                GetComponent<Image>().sprite = Utils.LoadSpriteByResouce(value);
            var shipData = m_ShipUIData as ShipUIButtonData;
            if (Utils.CheckReferenceIsNull(shipData))
                return ;
            shipData.m_IconPath = value;
        } 
    }

    public int bindKey
    {
        get
        {
            var shipData = m_ShipUIData as ShipUIButtonData;
            if (Utils.CheckReferenceIsNull(shipData))
                return -1;
            return shipData.m_BindKey;
        }
        set
        {
            var shipData = m_ShipUIData as ShipUIButtonData;
            if (Utils.CheckReferenceIsNull(shipData))
                return;
            shipData.m_BindKey = value;
        }
    }
    public UnityEvent clickEvent = new UnityEvent();
    public UnityEvent pressEvent = new UnityEvent();
    public UnityEvent releaseEvent = new UnityEvent();
    public UnityEvent pressDownEvent = new UnityEvent();

    public ShipUIButton()
    {
        m_Type = ShipUIType.Button;
        m_ShipUIData = ShipUIManager.Instance.Create((int)m_Type);
    }
    private void Update()
    {
        if (isPressDown)
        {
            if (Time.time - invokeTime > 0.1f)
            {
                //触发点击;
                OnPress();
                invokeTime = Time.time;
            }
        }
            
        var shipUIData = m_ShipUIData as ShipUIButtonData;
        if(shipUIData.m_BindKey != 0)
        {
            if(Input.GetKeyDown((KeyCode)shipUIData.m_BindKey))
                pressDownEvent.Invoke();
            if (Input.GetKey((KeyCode)shipUIData.m_BindKey))
                pressEvent.Invoke();
            if (Input.GetKeyUp((KeyCode)shipUIData.m_BindKey))
                releaseEvent.Invoke();
            if (Input.GetKeyDown((KeyCode)shipUIData.m_BindKey))
                clickEvent.Invoke();


            //if (Input.GetMouseButton(shipUIData.m_BindKey))
            //    pressDownEvent.Invoke();
            //if (Input.GetKey(shipUIData.m_BindKey))
            //    pressEvent.Invoke();
            //if (Input.GetKeyUp((KeyCode)shipUIData.m_BindKey))
            //    releaseEvent.Invoke();
            //if (Input.GetKeyDown((KeyCode)shipUIData.m_BindKey))
            //    clickEvent.Invoke();
        }
    }

    public override string GetTypeName()
    {
        return "按钮";
    }
    
    public override string GetOperationName(int operation)
    {
        string name = "None";
        switch((OperationType)operation)
        {
            case OperationType.Press:
                name = "按住";
                break;
            case OperationType.PressDown:
                name = "按下";
                break;
            case OperationType.Release:
                name = "松开";
                break;
            case OperationType.Click:
                name = "点击";
                break;
        }
        return name;
    }
    public override void ApplyData()
    {
        base.ApplyData();
        var shipUIData = m_ShipUIData as ShipUIButtonData;
        iconPath = shipUIData.m_IconPath;
    }
    public override UnityEvent GetOperationEvent(int operationType)
    {
        UnityEvent selectedEvent = null;
        switch ((OperationType)operationType)
        {
            case OperationType.Press:
                selectedEvent = clickEvent;
                break;
            case OperationType.PressDown:
                selectedEvent = pressEvent;
                break;
            case OperationType.Release:
                selectedEvent = releaseEvent;
                break;
            case OperationType.Click:
                selectedEvent = pressDownEvent;
                break;
        }
        return selectedEvent;
    }
    bool isPressDown = false;
    float invokeTime = 0;

    public void OnPress()
    {
        pressEvent.Invoke();
    }
    public void OnPressDown()
    {
        isPressDown = true;
        pressDownEvent.Invoke();
        invokeTime = Time.time;
        OnPress();
    }
    public void OnRelease()
    {
        isPressDown = false;
        releaseEvent.Invoke();
    }
    public void OnClicked()
    {
        clickEvent.Invoke();
    }
}